

// 获取当前浏览器的agent
export const getAgent = () => {
  const ua = navigator.userAgent;
  
  if(ua.indexOf('Alipay') > -1) {
    return 'alipay'
  } else if(ua.indexOf('MicroMessenger') > -1){
    return 'wx'
  } else if(ua.indexOf('Firefox') > -1){
    return 'firefox'
  } else if(ua.indexOf('Chrome') > -1){
    return 'chrome'
  } else {
    return 'other'
  }

};


export const closeWindow = () => {
  const agent = getAgent();
  if(agent == 'wx'){
    // WeixinJSBridge 在微信公众号中自动引入
    (window as any).WeixinJSBridge.call('closeWindow');
  } else if(agent == 'alipay') {
    // 注入支付宝jsapi
    const oScript = document.createElement('script');
    oScript.type = 'text/javascript';
    oScript.src = 'https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.min.js';
    document.body.appendChild(oScript);
    oScript.onload = () => {
      (window as any).ap.popWindow();
    };
  } else if(agent == 'firefox' || agent == 'chrome') {
    window.location.href = "about:blank";
    window.close();
  } else{
    window.opener = null;
    window.open("", "_self");
    window.close();
  }
  
};